<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>卡片名称</span>
        <el-button type="primary" @click="addDiog">添加按钮</el-button>
      </div>
      <div>
        <el-table :data="cataList" border style="width: 100%">
          <el-table-column type="index" prop="date" label="序号" width="100%">
          </el-table-column>
          <el-table-column prop="cate_name" label="分类别名">
          </el-table-column>
          <el-table-column prop="cate_alias" label="分类别名">
          </el-table-column>
          <el-table-column label="操作">
            <template v-slot=scoped>
            <el-button type="primary" size="mini" @click="updateCateBtnFn(scoped.row)">修改</el-button>
            <el-button type="danger" size="mini" @click="delCateFn(scoped.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>

    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%" @close="dialogCloseFn">
          <el-form :model="addForm" :rules="addRules" ref="addRef" label-width="80px">
      <el-form-item label="分类名称" prop="cate_name">
        <el-input v-model="addForm.cate_name" minlength="1" maxlength="10"></el-input>
      </el-form-item>
      <el-form-item label="分类别名" prop="cate_alias">
        <el-input v-model="addForm.cate_alias" minlength="1" maxlength="15"></el-input>
      </el-form-item>
    </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelFn">取 消</el-button>
        <el-button type="primary" @click="confirmFn">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { getArtCateList, saveArtCateList, updateArtCateAPI, delArtCateAPI } from '@/api/index'
export default {
  name: 'ArtCate',
  created () {
    this.getArtList()
  },
  data () {
    return {
      cataList: [],
      dialogVisible: false,
      addForm: { // 添加表单的数据对象
        cate_name: '',
        cate_alias: ''
      },
      addRules: { // 添加表单的验证规则对象
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名必须是1-10位的非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
        ]
      },
      isEdit: false,
      editId: ''
    }
  },
  methods: {
    async getArtList () {
      const res = await getArtCateList()
      console.log(res)
      this.cataList = res.data.data
    },
    handleClose (done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => { })
    },
    addDiog () {
      this.isEdit = false
      this.dialogVisible = true
    },
    cancelFn () {
      this.dialogVisible = false
    },
    confirmFn () {
      this.$refs.addRef.validate(async valid => {
        if (valid) {
          //
          if (this.isEdit) {
            //
            this.addForm.id = this.editId
            const { data: res } = await updateArtCateAPI(this.addForm)
            if (res.code !== 0) this.$message.error(res.message)
            this.$message.success(res.message)
            console.log(res, '修改修改修改修改')
          } else {
            const { data: res } = await saveArtCateList(this.addForm)
            if (res.code !== 0) this.$message.error(res.message)
            this.$message.success(res.message)
            console.log(res, '新增新增新增新增')
          }
          this.getArtList()
          this.dialogVisible = false
        } else {
          return false
        }
      })
    },
    dialogCloseFn () {
      this.$refs.addRef.resetFields()
    },
    updateCateBtnFn (obj) {
      console.log(obj)
      this.dialogVisible = true
      this.isEdit = true
      this.editId = obj.id
      this.$nextTick(() => {
        this.addForm.cate_alias = obj.cate_alias
        this.addForm.cate_name = obj.cate_name
      })
    },
    async delCateFn (obj) {
      const { data: res } = await delArtCateAPI(obj.id)
      console.log(res)
      if (res.code !== 0) return this.$message.error(res.message)
      this.$message.success(res.message)
      this.getArtList()
    }
  }
}
</script>
<style>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
